package com.jacen.fzxs.service.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jacen.fzxs.dao.IBaseDao;
import com.jacen.fzxs.dao.IRoleUserDao;
import com.jacen.fzxs.dao.IUserDao;
import com.jacen.fzxs.entity.RoleUserEntity;
import com.jacen.fzxs.entity.UserEntity;
import com.jacen.fzxs.service.IRoleUserService;
import com.jacen.fzxs.util.EntityUtils;
import com.jacen.fzxs.web.Pager;

/**
 * 
 * @author Jacen
 *
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RoleUserService extends BaseService<RoleUserEntity> implements IRoleUserService {
	@Resource
	private IRoleUserDao roleUserDao;
	@Resource
	private IUserDao userDao;

	@Override
	public IBaseDao<RoleUserEntity> getDao() {
		return roleUserDao;
	}

	@Override
	public void updateUserByRole(String roleId, String select) throws Exception {
		String[] ids = select.split(",");
		RoleUserEntity entity = null;
		List<RoleUserEntity> list = new ArrayList<RoleUserEntity>();
		for(String id : ids){
			entity = new RoleUserEntity();
			entity.setId(EntityUtils.createId32());
			entity.setRoleId(roleId);
			entity.setUserId(id);
			list.add(entity);
		}
		roleUserDao.insert(list);
	}

	@Override
	public void deleteRoleUser(String roleId, String userId) {
		roleUserDao.deleteRoleUser(roleId,userId);
	}

	@Transactional(readOnly = true)
	public List<UserEntity> getPage(Pager pager) throws SQLException {
		pager.setTotalCount(getCount());
		return userDao.getPage((pager.getCurPageNum()-1) * pager.getPageSize() , pager.getPageSize());
	}
	
	@Transactional(readOnly = true)
	public int getCount() throws SQLException {
		return userDao.getCount();
	}


}
